﻿using CoreCms.Net.Caching.AccressToken;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.WeChat.Service.HttpClients;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
using System.Threading.Tasks;
using SKIT.FlurlHttpClient.Wechat.Api;
using CoreCms.Net.Model.WeChatEntites;

namespace CoreCms.Net.Web.WebApi.Controllers
{
    /// <summary>
    /// 小程序内置直播功能
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LiveBroadCastController : ControllerBase
    {
        private readonly IWeChatApiHttpClientFactory _weChatApiHttpClientFactory;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="weChatApiHttpClientFactory"></param>
        public LiveBroadCastController(IWeChatApiHttpClientFactory weChatApiHttpClientFactory)
        {
            _weChatApiHttpClientFactory = weChatApiHttpClientFactory;
        }


        #region 获取直播间列表和回放
        /// <summary>
        /// 获取直播间列表和回放
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetLiveInfo([FromBody] FMPageByIntId entity)
        {
            var jm = new WebApiCallBack();

            var client = _weChatApiHttpClientFactory.CreateWxOpenClient();
            var accessToken = WeChatCacheAccessTokenHelper.GetWxOpenAccessToken();

            var request = new MyWxaBusinessGetLiveInfoRequest
            {
                AccessToken = accessToken,
                Limit = entity.limit,
                Offset = (entity.page - 1) * entity.limit,
                Action = !string.IsNullOrEmpty(entity.where) ? "get_replay" : ""
            };

            var response = await client.ExecuteWxaBusinessGetLiveInfoAsync(request);
            if (response.IsSuccessful())
            {
                jm.status = true;
                jm.data = response;
            }
            else
            {
                jm.status = false;
                jm.msg = response.ErrorCode == 9410000 ? "暂无直播信息" : response.ErrorMessage;
                jm.otherData = response;
            }
            return jm;
        }
        #endregion




    }
}
